The Asset Controller (attached to Gameboard) exposes all the asset features available to creators. The asset controller is how we exchange assets between the game and the Companions. All assets from backgrounds, to card faces, to controllers are sent using this controller.
There are two steps to get assets onto companion:
CompanionAsset
to the CompanionAssets
list within the AssetsControllerVideo Walkthrough:
The Gameboard prefab is always available in your game as long as the SDK has been integrated. In order to access it from our custom Mono Behavior we just need to find it by its tag.
GameObject gameboardObject = GameObject.FindWithTag("Gameboard");
With access to our Gameboard object we can then move on to obtain a reference to the Asset Controller. This is the controller that handles all the functions related to assets between the game and companion.
Define an AssetController so we may later access it anywhere in the script:
AssetController assetController;
And request the controller from the Gameboard object:
assetController = gameboardObject.GetComponent<AssetController>();
The Asset Controller needs to have the assets as a CompanionAsset
to be able to send it to a Companion. Here are a few ways to add textures to the AssetController.
To load a single asset, you can use AddTextureToAssets
on a Texture2D object or AddFBXToAssets
for a Mesh. Calling either of these will generate a CompanionAsset
(CompanionTextureAsset or CompanionFBXAsset accordingly) and store it within the CompanionAssets
list on the AssetController.
Assume texture
is a Texture2D object.
CompanionTextureAsset asset = assetController.AddTextureToAssets(texture);
CompanionFBXAsset asset = assetController.AddFBXToAssets(texture);
CompanionTextureAsset asset = assetController.CreateTextureAsset(texture);
CompanionFBXAsset asset = assetController.CreateFBXAsset(texture);
You can also manually create the assets by creating a new CompanionTextureAsset
or CompanionFBXAsset
if you want to manually specify a name other than that objects file name. Creating the assets this way will also add them to the CompanionAssets
list, making them ready to load onto a Companion.
byte[] imageBytes = t.EncodeToPNG();
CompanionTextureAsset asset = new CompanionTextureAsset(imageBytes, assetController, "differentName");
If you have a folder in your project and you just want to load all the assets from that folder to the AssetController, you can use CreateCompanionAssetsFromPath.
First you need to specify which types of files you are loading with the AddAsset
delegate. We need to send a method that each file in the path is put through to create CompanionAsset
objects.
In this example, the Cards folder contains png files of card images. So I create the texture delegate passing the AddTextureToAsset method so each of those textures is turned into the correct CompanionAsset
, CompanionTextureAsset in this case. Then, we pass the texture delegate to the CreateCompanionAssetsFromPath method to get a list of all the files in that path as a CompanionTextureAsset
.
var addTextureDelegate = new AssetController.AddAsset<CompanionTextureAsset, Texture2D>(assetController.AddTextureToAssets);
List<CompanionTextureAsset> textureAssetsFromPath = assetController.CreateCompanionAssetsFromPath("Cards", textureDelegate);
var addTextureDelegate = new AssetController.AddAsset<CompanionTextureAsset, Texture2D>(assetController.CreateTextureAsset);
List<CompanionTextureAsset> textureAssetsFromPath = assetController.CreateCompanionAssetsFromPath("Cards", textureDelegate);
Now you have all the files from that folder in the CompanionAssets list on the AssetController, and they are ready to be loaded onto a Companion.
You can also add assets to be loaded into the AssetController through publically accessable TextureAssets list.
To access this, go to the Gameboard prefab object, expand the Asset Controller script and add 2D textures to the TextureAssets list.
These will be added and accessable now in the AssetController.
TIP: Make sure the file names are unique, otherwise accessing the assets through your scripts could be difficult.
If the files do not have unique names, it is better to load them individually (as descripted in the above section) so you can keep track of the assets or guid associated to each specific texture.
If for some reason you no longer want an asset to be in the CompanionAssets and readily available to load onto a Companion, you can remove it with RemoveAssetFromAssets (with v2.0.1+)
In this example, image existingAsset is some ICompanionAsset object that you have already loaded into the AssetController.
assetController.RemoveAssetFromAssets(existingAsset.AssetGuid);
Before v2.0.1, you can manually remove assets from the CompanionAssets list.
assetController.CompanionAssets.Remove(existingAsset.AssetGuid);
You can access the assets in the AssetController through the CompanionAssets
dictionary by the assets guid, or the CompanionAssetsByName
dictionary by the assets name.
The name will be the files name. The CompanionAssetsByName
is only reliable if all the assets uploaded have unique names.
Now that we have all the assets we need converted to CompanionAssets and stored in the AssetController, we want to load those assets onto the connected Companions.
We have the following methods available:
CompanionAsset
object)CompanionAsset
object)Both LoadAllAsset methods will take the assets stored in the AssetController and load them onto Companions. You can use LoadAllAssetsOntoAllCompanions
to load onto all Companions initially, and LoadAllAssetsOntoOneCompanion
to reload assets if a companion is disconnect and rejoins, or to add as Companions connect to the board.
assetControler.LoadAllAssetsOntoAllCompanions();
assetController.LoadAllAssetsOntoOneCompanion("userABC");
If you want to load a single asset to Companion before a specific action, you can use the method LoadAssetToCompanion that is found on the create CompanionAsset
object, or the LoadAssetToCompanion method in the AssetController (only available in v2.0.1+).
CompanionTextureAsset asset = assetController.AddTextureToAssets(texture);
var response = assetController.LoadAssetToCompanion(asset, "userABC"); //Checks that the user exists and is a companion presence
//or
var response = assetController.LoadAssetToCompanion(asset.AssetGuid, "userABC"); //Checks that the user exists and is a companion presence
//or
var response = asset.LoadAssetToCompanion("userABC"); //Sends asset without user checks
If you want to load an individual asset onto a Companion, but you don't want it loaded with all the assets in the LoadAll methods you can set the loadWithLoadAll
flag to false when adding the asset to the controller. This will exclude the asset when loading all to companion and allow you to load specific assets on demand as wanted.
CompanionTextureAsset asset = assetController.AddTextureToAssets(texture, false);
var response = asset.LoadAssetToCompanion("userABC");
CompanionTextureAsset asset = assetController.CreateTextureAsset(texture);
var response = asset.LoadAssetToCompanion(userPresenceController, "userABC"); //Checks that the user exists and is a companion presence
You can remove individual assets from a Companion.
This removes the Asset from the Companion device, but the asset is still available in the CompanionAsset
list and will be loaded back if either of the LoadAll methods are called.
CompanionTextureAsset asset = assetController.AddTextureToAssets(texture);
CompanionMessageResponseArgs response = await asset.DeleteAssetFromCompanion("userABC");
List<CompanionMessageResponseArgs> response = await asset.DeleteAssetFromAllCompanions();
CompanionTextureAsset asset = assetController.CreateTextureAsset(texture);
CompanionMessageResponseArgs response = await assetController.DeleteAssetFromCompanion("userABC");
List<CompanionMessageResponseArgs> response = await asset.DeleteAssetFromAllCompanions();
This section include the entire code in one single, easy to copy section.
AssetController assetController;
// Start is called before the first frame update
void Start()
{
GameObject gameboardObject = GameObject.FindWithTag("Gameboard");
assetController = gameboardObject.GetComponent<AssetController>();
}
public void SendSingleAssetToCompanion(Texture2D texture, string userIdToSendTo)
{
CompanionTextureAsset asset = assetController.AddTextureToAssets(texture);
asset.LoadAssetToCompanion(userId);
}
public void AddAllCardsToAssets()
{
var addTextureDelegate = new AssetController.AddAsset<CompanionTextureAsset, Texture2D>(assetController.AddTextureToAssets);
List<CompanionTextureAsset> textureAssetsFromPath = assetController.CreateCompanionAssetsFromPath("Cards", textureDelegate);
}
public void LoadToAllCompanions()
{
assetController.LoadAllAssetsOntoAllCompanions();
}
public void LoadToOneCompanions(string userId)
{
assetController.LoadAllAssetsOntoOneCompanion();
}